#author Devdoer<devdoer@gmail.com>
import thread
class SingletonMeta(type):
	def __init__(cls,name,base,dic):
		super(SingletonMeta,cls).__init__(name,base,dic)
		cls.s=None
	def __call__(cls,*arg,**keyargs):
		if cls.s==None:
			cls.s=super(SingletonMeta,cls).__call__(*arg,**keyargs)
		return cls.s    
class Singleton:
	__metaclass__=SingletonMeta
class PerThreadSingletonMeta(type):
	def __init__(cls,name,base,t):
		cls.__instance={}
		cls._lock=thread.allocate_lock()
		type.__init__(cls,name,base,t)
	def __call__(cls,*args,**keyargs):
		id=thread.get_ident()
		if not cls.__instance.has_key(id):
			cls._lock.acquire()
			#cls.__instance[id]=instan=cls.__new__(cls)
			#cls.__init__(instan,*args,**keyargs)
			cls.__instance[id]=super(PerThreadSingletonMeta,cls).__call__(*args,**keyargs)
			cls._lock.release()

		return cls.__instance[id]  
class PerThreadSingleton(object):
	__metaclass__=PerThreadSingletonMeta    
	
